package com.example.administrator.shzs.fragment.shopdetails;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.shzs.R;
import com.example.administrator.shzs.utils.ToastUtils;
import com.example.administrator.shzs.adapter.EvaluateListAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.entity.EvaluateData.EvaluateList;
import com.example.administrator.shzs.parse.ParseEvaluateList;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 评价页面
 */
public class EvaluateFragment extends Fragment {


    @Bind(R.id.mXecyclerView)
    XRecyclerView mXecyclerView;
    @Bind(R.id.rl_evaluate)
    RelativeLayout rlEvaluate;

    private List<EvaluateList> evaluateLists;
    private List<EvaluateList> moreDatas;
    private int page = 1;


    private EvaluateListAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_evaluate, null);
        ButterKnife.bind(this, view);
        setXRecyclerView();

        getDataFormSever(1);

        //下拉刷新上拉加载监听
        setListeners();
        return view;


    }

    private void getDataFormSever(final int page) {

        String url = Contents.EVALUATE_LIST_URL + "?shopid=" + MyApplication.app.getShopId() + "&pagesize=20&pageindex=" + page;

        RequestParams params = new RequestParams(url);

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {

                    if (page == 1) {
                        evaluateLists = ParseEvaluateList.getEvaluateListData(result);
                        if (evaluateLists.size() == 0) {

                            rlEvaluate.setVisibility(View.VISIBLE);
                            mXecyclerView.setVisibility(View.GONE);
                            return;
                        }
                        setAdapter();
                    } else {

                        moreDatas = ParseEvaluateList.getEvaluateListData(result);
                        if (moreDatas.size() == 0) {
                            ToastUtils.showMessageShort(getActivity(), "没有更多了哦");
                            return;
                        }
                        evaluateLists.addAll(moreDatas);
                        moreDatas.clear();
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    /**
     * 设置Adapter
     */
    private void setAdapter() {

        adapter = new EvaluateListAdapter(getActivity(), evaluateLists);
        mXecyclerView.setAdapter(adapter);
    }


    private void setListeners() {

        mXecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getDataFormSever(1);
                        mXecyclerView.refreshComplete();
                    }
                },500);

            }

            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getDataFormSever(page);
                        mXecyclerView.loadMoreComplete();
                    }
                },500);

            }
        });
    }


    /**
     * 设置XRecyclerView
     */
    private void setXRecyclerView() {

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayout.VERTICAL);
        mXecyclerView.setLayoutManager(manager);

        //设置可刷新
        mXecyclerView.setRefreshing(true);
        mXecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//刷新样式
        mXecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);//加载更多样式

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
